<template>
<el-container class="home-container">
  <el-aside :width="isCollapse?'64px':'200px'">
    <div class="title">
        <span>{{isCollapse ? '后台':'后台管理系统'}}</span>
      </div>
      <!-- 侧边栏菜单区域 -->
         <el-menu
      default-active="2"
      class="el-menu-vertical-demo"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#409EFF"
      unique-opened
      :collapse="isCollapse"
      :collapse-transition="false"
      router
    >
      <!-- 一级菜单 -->
      <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
         <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
           <i class="el-icon-menu"></i>
          <span>{{subItem.authName}}</span>
         </el-menu-item>
      </el-submenu>

    </el-menu>
  </el-aside>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
 <div class="l-content">
      <el-button plain icon="el-icon-menu" size="mini" @click="handleMenu"></el-button>
    </div>
    <div class="r-content">
      <el-dropdown trigger="click" szie="mini">
        <span>
           <img src="@/assets/logo.png" alt=""  class="user" />
        </span>
      <el-dropdown-menu slot="dropdown">
    <el-dropdown-item @click.native="onHome">个人中心</el-dropdown-item>
    <el-dropdown-item @click.native="onLogout">退出登录</el-dropdown-item>

  </el-dropdown-menu>

      </el-dropdown>
    </div>
    </el-header>
    <!-- 主体内容 -->
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>

export default {
  name: 'Mian',
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      iconsObj: {
        // 一级菜单的icon图标

        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-zhanghaoquanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujutongji'
      },
      isCollapse: false, // 是否折叠属性
      activePath: ''

    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    handleMenu () {
      this.isCollapse = !this.isCollapse
    },
    onHome () {
      this.$router.push('/home')
    },
    onLogout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮,切换菜单的折叠和展开
    // toggleCollpase () {
    //   this.isCollapse = !this.isCollapse
    // },
    saveActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
      // console.log(this.$route.path)
    }
  }

}
</script>

<style lang="less" scoped>
@import '@/assets/font/iconfont.css';

.home-container {
  height: 100vh;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  background-color: #fff;
  align-items: center;
  color: #fff;
  font-size: 20px;

}
.el-aside {
  background-color: #1D1D1D;
  height: 100vh;
  > div {
    display: flex;
    align-items: center;
    span {
      margin: 15px 0 0 15px;
      font-size: 20px;
    }
  }
  .el-submenu{
     background-color: #1D1D1D;
    /deep/.el-submenu__title,.el-menu-item{
background-color: #1D1D1D !important;
     }
  }

  .el-menu {
    border-right: none;
    margin-top:15px
  }

}
.el-main {
  background-color: #eaedf1;
}
.title{
  color:#fff;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background: #4a5064;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-header{

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.l-content{
  display: flex;
 margin-left: 10px;
}
.r-content{
  margin-right: 10px;
   .user{
     width: 40px;
     height: 40px;
     border-radius: 50%;
   }
}
</style>
